# -*- coding: utf-8 -*-
import time
import json

from scrapy import Request
from scrapy.selector import Selector

from amac_spider.items import FundAccountItem
from amac_spider.utils.my_redis_spider import RedisSpiderPro
from amac_spider.utils.handler import handle_url, get_url, get_datetime


# 基金公司及子公司集合资管产品公示
class FundAccountSpider(RedisSpiderPro):
    name = 'fund_account'

    def parse(self, response):
        item = FundAccountItem()
        info_list = json.loads(response.text)['content']
        for info in info_list:
            item['product_id'] = info['id']
            item['detail_url'] = handle_url(get_url(self.name), info['id'])
            item['crawl_time'] = get_datetime(time.time())
            yield Request(url=item['detail_url'], callback=self.parse_detail, meta={'item': item}, dont_filter=True)

    def parse_detail(self, response):
        item = response.meta['item']
        fields = ['product_name', 'product_code', 'manager', 'trustee', 'record_time',
                  'found_time', 'expire_time', 'investment_stat', 'divide_level', 'work_stat']
        selector = Selector(response)
        for index, field in enumerate(fields):
            item[field] = selector.xpath(f'//*[@class="table"]//tr[{index+1}]/td[2]/text()').extract_first()
        yield item
